import { http } from '@/utils/http'
import type { components } from '@/types/ApiInterface'

// 接口返回类型
export interface IBaseResponse<T = any> {
  code: number
  message: string
  data?: T
}

// 登录参数类型
export type LoginParams = components['schemas']['LoginDto']

// 登录返回类型
export type LoginResponse = components['schemas']['LoginResponse']

// 注册参数类型
export type RegisterParams = components['schemas']['RegisterDto']

// 发送短信验证码参数类型
export type SendSmsCodeParams = components['schemas']['SendSmsCodeDto']

// 用户社区信息类型
export type UserCommunityInfo = components['schemas']['CommunityUserStatus']

// 用户门禁权限返回类型
export type UserGatePermissionResponse = components['schemas']['GatePermissionUserResponse']

// 注册社区参数类型
export type RegisterCommunityParams = components['schemas']['CommunityUserStatusRegisterDto']

// 修改密码参数类型
export type ChangePasswordParams = components['schemas']['ChangePasswordDto']

// 新增或修改小区用户参数类型
export type UpsertCommunityUsersParams = components['schemas']['CommunityUserStatusUpsertDto']

/**
 * 登录
 */
export const loginAPI = (params: LoginParams) => {
  return http.post<LoginResponse>('/api/iam/login', params)
}

/**
 * 注册
 */
export const registerAPI = (params: RegisterParams) => {
  return http.post<IBaseResponse<LoginResponse>>('/api/iam/register', params)
}

/**
 * 直接注册入小区
 */
export const registerCommunityAPI = (params: RegisterCommunityParams) => {
  return http.post<IBaseResponse<any>>('/api/iam/registerCommunityUser', params)
}

/**
 * 发送短信验证码
 */
export const sendSmsCodeAPI = (params: SendSmsCodeParams) => {
  return http.post<IBaseResponse<string>>('/api/iam/sendSmsCode', params)
}

/**
 * 获取用户社区列表
 */
export const getUserCommunitiesAPI = () => {
  return http.get<IBaseResponse<UserCommunityInfo[]>>('/api/user/detail')
}

/**
 * 获取用户门禁权限
 */
export const getUserGatePermissionAPI = (id: string) => {
  return http.get<UserGatePermissionResponse[]>(`/api/gatePermission/user-gate-permission?id=${id}`)
}

/**
 * 开门
 */
export const openDoorAPI = (deviceId: string, communityId: string) => {
  return http.post<IBaseResponse<string>>(`/api/gatePermission/user-open-gate`, {
    deviceId,
    communityId,
  })
}

/**
 * 关门
 */
export const closeDoorAPI = (deviceId: string, communityId: string) => {
  return http.post<IBaseResponse<string>>(`/api/gatePermission/user-close-gate`, {
    deviceId,
    communityId,
  })
}

/**
 * 修改密码
 */
export const changePasswordAPI = (params: ChangePasswordParams) => {
  return http.post<IBaseResponse<any>>('/api/iam/changePassword', params)
}

/**
 * 获取门禁卡列表
 */
export const getGateCardsAPI = () => {
  return http.get<IBaseResponse<any>>('/api/gateCard/list')
}

/**
 * 删除用户
 */
export const deleteCommunityUserAPI = (id: string) => {
  return http.post<IBaseResponse<any>>(`/api/communityUserStatus/delete`, {
    id,
  })
}

// 新增或修改小区用户
export const upsertCommunityUsersAPI = (params: UpsertCommunityUsersParams) => {
  return http.post<IBaseResponse<any>>(`/api/communityUserStatus/upsert`, params)
}
